<template>
  <div class="container">
    <n-card :bordered="false" class="proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
        <template #statusSlot="{ model, field }">
          <n-input v-model:value="model[field]" />
        </template>
      </BasicForm>

      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="actionRef"
        :bordered="false"
        :single-line="false"
        :actionColumn="actionColumn"
        @update:checked-row-keys="onCheckedRow"
        :scroll-x="1800"
      >
        <template #tableTitle>
          <n-button type="primary" @click="openCreateDrawer">
            <template #icon>
              <n-icon>
                <PlusOutlined />
              </n-icon>
            </template>
            新建
          </n-button>
        </template>

        <template #toolbar>
          <n-button type="primary" @click="reloadTable">刷新数据</n-button>
        </template>
      </BasicTable>
    </n-card>
    <CreateDrawer
      ref="createDrawerRef"
      :reload-table="reloadTable"
      :title="createDrawerTitle"
      :width="750"
    />
    <EditDrawer
      ref="editDrawerRef"
      :title="eidterDrawerTitle"
      :width="750"
      :reload-table="reloadTable"
    />
  </div>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getMaterialPage, deleteMaterial } from '@/api/material/material';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';
  import EditDrawer from './EditDrawer.vue';
  import { useDialog, useMessage } from 'naive-ui';

  const dialog = useDialog();
  const message = useMessage();

  const createDrawerTitle = ref('新建素材');
  const eidterDrawerTitle = ref('编辑素材');
  const createDrawerRef = ref();
  const editDrawerRef = ref();

  // 搜索
  const schemas: FormSchema[] = [
    {
      field: 'title',
      labelMessage: '输入标题',
      component: 'NInput',
      label: '标题',
      componentProps: {
        placeholder: '请输入标题',
        onInput: (e: any) => {
          console.log(e);
        },
      },
      rules: [{ required: true, message: '输入标题', trigger: ['blur'] }],
    },
    {
      field: 'tag',
      component: 'NInputNumber',
      label: '标签',
      componentProps: {
        placeholder: '请输入标签',
        showButton: false,
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeDate',
      component: 'NDatePicker',
      label: '预约时间',
      defaultValue: 1183135260000,
      componentProps: {
        type: 'date',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'state',
      component: 'NSelect',
      label: '状态',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          {
            label: '正常',
            value: 1,
          },
          {
            label: '停用',
            value: 0,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const actionRef = ref();
  const formParams = reactive({
    name: '',
    address: '',
    date: null,
  });

  const params = ref({
    pageSize: 5,
    name: 'xiaoMa',
  });

  const actionColumn = reactive({
    width: 100,
    title: '操作',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '编辑',
            onClick: openEditDrawer.bind(null, record),
          },
          {
            label: '删除',
            onClick: handleDelete.bind(null, record),
          },
        ],
        select: (key) => {
          window['$message'].info(`您点击了，${key} 按钮`);
        },
      });
    },
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  const loadDataTable = async (res) => {
    return await getMaterialPage({ ...formParams, ...params.value, ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function openCreateDrawer() {
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  function openEditDrawer(record: Recordable) {
    const { openDrawer } = editDrawerRef.value;
    console.log(record);
    openDrawer(record);
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    dialog.info({
      title: '警告',
      content: '你确定要删除此素材？',
      positiveText: '确定',
      negativeText: '取消',
      onPositiveClick: async () => {
        await deleteMaterial(record.id);
        reloadTable();
        message.success('删除成功！');
      },
      onNegativeClick: () => {
        // message.error('不确定');
      },
    });
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped></style>
